<template>
  <div>
    <a-form
      ref="editRef"
      :model="editForm"
      :rules="rules1"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item label="角色名称" name="roleName">
        <a-input
          placeholder="请输入角色名称"
          v-model:value="editForm.roleName"
        ></a-input>
      </a-form-item>
      <a-form-item label="角色描述" name="roleDesc">
        <a-input
          placeholder="请输入角色描述"
          v-model:value="editForm.roleDesc"
        ></a-input>
      </a-form-item>
      <a-form-item label="功能权限">
        <a-checkbox @change="handleCheckAll">全选</a-checkbox>
      </a-form-item>
    </a-form>
    <a-row>
      <a-col :span="12" :offset="4">
        <a-tree
          v-model:expandedKeys="expandedKeys"
          v-model:selectedKeys="selectedKeys"
          v-model:checkedKeys="checkedKeys"
          checkable
          :tree-data="treeData"
          @check="handleCheck"
        ></a-tree>
      </a-col>
    </a-row>
    <a-row style="margin-top: 60px">
      <a-col :span="4" :offset="4" v-if="title !== '查看管理员'">
        <a-button type="primary" @click="save" class="d-a-center"
          >保存</a-button
        >
      </a-col>
      <a-col :span="4" :offset="title == '查看管理员' ? 10 : 2">
        <a-button type="info" @click="cancle" class="d-a-center">返回</a-button>
      </a-col>
    </a-row>
  </div>
</template>
<script>
import {
  reactive,
  ref,
  watch,
  computed,
  defineComponent,
  createVNode,
  toRefs,
  onMounted,
} from "vue";
import {
  CheckOutlined,
  EditOutlined,
  ExclamationCircleOutlined,
} from "@ant-design/icons-vue";
import { message, Modal } from "ant-design-vue";
import { useRoute, useRouter } from "vue-router";
import { menuList, saveMenu, roleDetail } from "@/api/setting/index";
import {
  validatorPass,
  validtorConfirmPass,
  validatorPhone,
} from "@/assets/js/validate";
import { template } from "lodash";
import { useModal } from "@/hooks"
export default {
  setup() {
    const router = useRouter();
    const route = useRoute();
    const formRef = ref("");
    const form = reactive({});
    const data = reactive({
      list: [], //管理员列表是
      id: "", //管理员ID
      editForm: {},
      finalTree: [],
      treeData: [
        {
          path: "/console",
          name: "Console",
          meta: {
            title: "首页",
            hidden: false,
          },
          redirect: "/home",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/home",
              name: "Home",
              meta: {
                title: "首页",
                hidden: false,
              },
            },
          ],
        },
        {
          path: "/content",
          name: "Content",
          meta: {
            title: "内容管理",
            hidden: false,
          },
          redirect: "/content/service",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/content/service",
              name: "ContentService",
              meta: {
                title: "服务管理",
                hidden: false,
              },
              redirect: "/content/service/index",
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              children: [
                {
                  path: "/content/service/index",
                  name: "ContentServiceIndex",
                  meta: {
                    title: "服务项目",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/content/ali",
              name: "ContentAliOrder",
              meta: {
                title: "阿里门店管理",
                hidden: false,
              },
              redirect: "/content/ali/store",
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              children: [
                {
                  path: "/content/ali/storelist",
                  name: "ContentALiStoreList",
                  meta: {
                    title: "门店列表",
                    hidden: false,
                  },
                },
                {
                  path: "/content/ali/productlist",
                  name: "ContentALiProductList",
                  meta: {
                    title: "商品列表",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/content/material",
              name: "ContentMaterial",
              meta: {
                title: "耗材管理",
                hidden: false,
              },
              redirect: "/content/material/list",
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              children: [
                {
                  path: "/content/material/list",
                  name: "ContentMaterialList",
                  meta: {
                    title: "耗材列表",
                    hidden: false,
                  },
                },
                {
                  path: "/content/material/price",
                  name: "ContentMaterialPrice",
                  meta: {
                    title: "价格管理",
                    hidden: true,
                  },
                },
                {
                  path: "/content/material/recycle",
                  name: "ContentMaterialRecycle",
                  meta: {
                    title: "耗材回收站",
                    hidden: true,
                  },
                },
              ],
            },
            {
              path: "/content/swiper",
              name: "ContentSwiper",
              meta: {
                title: "轮播图管理",
                hidden: false,
              },
              redirect: "/content/swiper/list",
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              children: [
                {
                  path: "/content/swiper/list",
                  name: "ContentSwiperList",
                  meta: {
                    title: "轮播图",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/content/know",
              name: "ContentKnow",
              meta: {
                title: "知识库管理",
                hidden: false,
              },
              redirect: "/content/know/list",
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              children: [
                {
                  path: "/content/know/list",
                  name: "ContentKnowList",
                  meta: {
                    title: "知识库",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/order",
          name: "order",
          meta: {
            title: "订单管理",
            hidden: false,
            roles: ["editor"],
          },
          redirect: "/order/center",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/order/center",
              name: "OrderCenter",
              meta: {
                title: "订单中心",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/order/center/all",
              children: [
                {
                  path: "/order/center/all",
                  name: "OrderIndex",
                  meta: {
                    title: "服务订单",
                    hidden: false,
                    keepAlive: true,
                  },
                },
                {
                  path: "/order/center/consume",
                  name: "OrderConsume",
                  meta: {
                    title: "耗材订单",
                    hidden: false,
                  },
                },
                {
                  path: "/order/center/offline",
                  name: "OrderOffline",
                  meta: {
                    title: "线下订单",
                    hidden: false,
                  },
                },
                {
                  path: "/order/center/offline-check",
                  name: "OrderOfflineCheck",
                  meta: {
                    title: "线下核对",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/order/refund",
              name: "OrderRefund",
              meta: {
                title: "退款单",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/order/refund/list",
              children: [
                {
                  path: "/order/refund/list",
                  name: "OrderRefundList",
                  meta: {
                    title: "退款服务",
                    hidden: false,
                  },
                },
                {
                  path: "/order/refund/ali",
                  name: "OrderRefundAli",
                  meta: {
                    title: "阿里退款单",
                    hidden: false,
                  },
                },
                {
                  path: "/order/refund/consume",
                  name: "OrderRefundConsume",
                  meta: {
                    title: "退款耗材",
                    hidden: false,
                  },
                },
                {
                  path: "/order/refund/rule",
                  name: "OrderRefundRule",
                  meta: {
                    title: "退款单规则",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/order/promotion",
              name: "OrderPromotion",
              meta: {
                title: "活动订单",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/order/promotion/group",
              children: [
                {
                  path: "/order/promotion/group",
                  name: "OrderPromotionGroup",
                  meta: {
                    title: "拼团活动",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/business",
          name: "Business",
          meta: {
            title: "业务管理",
            hidden: false,
          },
          redirect: "/business/audit",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/business/audit",
              name: "BusinessAudit",
              meta: {
                title: "护理报告审核",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/business/audit/index",
              children: [
                {
                  path: "/business/audit/index",
                  name: "BusinessAuditIndex",
                  meta: {
                    title: "审核列表",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/business/certification",
              name: "BusinessCertification",
              meta: {
                title: "认证管理",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/business/certification/nurse",
              children: [
                {
                  path: "/business/certification/nurse",
                  name: "BusinessCertificationNurse",
                  meta: {
                    title: "护士认证",
                    hidden: false,
                  },
                },
                {
                  path: "/business/certification/accompany",
                  name: "BusinessCertificationAccompany",
                  meta: {
                    title: "陪护认证",
                    hidden: true,
                  },
                },
              ],
            },
            {
              path: "/business/widthdraw/list",
              name: "widthdrawlist",
              meta: {
                title: "提现管理",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/business/widthdraw/list",
              children: [
                {
                  path: "/business/widthdraw/list",
                  name: "widthdrawWidthdrawList",
                  meta: {
                    title: "提现详情列表",
                    hidden: false,
                  },
                },
                {
                  path: "/business/widthdraw/setting",
                  name: "widthdrawWidthdrawSetting",
                  meta: {
                    title: "提现设置",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/user",
          name: "User",
          meta: {
            title: "用户管理",
            hidden: false,
          },
          redirect: "/user/nurse",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/user/nurse",
              name: "UserNurse",
              meta: {
                title: "护士端",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/user/nurse/index",
              children: [
                {
                  path: "/user/nurse/index",
                  name: "UserNurseIndex",
                  meta: {
                    title: "全部护士",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/user/client",
              name: "UserClient",
              meta: {
                title: "用户端",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/user/client/index",
              children: [
                {
                  path: "/user/client/index",
                  name: "UserClientIndex",
                  meta: {
                    title: "全部用户",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/analysis",
          name: "Analysis",
          meta: {
            title: "数据分析",
            hidden: false,
          },
          redirect: "/analysis/detail",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/analysis/detail",
              name: "analysisDetail",
              meta: {
                title: "基础数据",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/analysis/detail/user",
              children: [
                {
                  path: "/analysis/detail/user",
                  name: "analysisDetailUser",
                  meta: {
                    title: "用户数据",
                    hidden: false,
                  },
                },
                {
                  path: "/analysis/detail/nurse",
                  name: "analysisDetailNurse",
                  meta: {
                    title: "护士数据",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/market",
          name: "Market",
          meta: {
            title: "营销管理",
            hidden: false,
          },
          redirect: "/market/detail",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/market/detail",
              name: "marketDetail",
              meta: {
                title: "营销中心",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/market/detail/list",
              children: [
                {
                  path: "/market/detail/list",
                  name: "marketDetailList",
                  meta: {
                    title: "优惠券",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/setting",
          name: "Setting",
          meta: {
            title: "系统设置",
            hidden: false,
          },
          redirect: "/setting/basic",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/setting/basic",
              name: "SettingBasic",
              meta: {
                title: "基础设置",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/setting/basic/notice",
              children: [
                {
                  path: "/setting/basic/notice",
                  name: "SettingBasicNotice",
                  meta: {
                    title: "系统通知",
                    hidden: false,
                  },
                },
                {
                  path: "/setting/basic/msg",
                  name: "SettingBasicMsg",
                  meta: {
                    title: "短信管理",
                    hidden: false,
                  },
                },
                {
                  path: "/setting/basic/push",
                  name: "SettingPushIndex",
                  meta: {
                    title: "push通知",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/setting/update",
              name: "SettingUpdate",
              meta: {
                title: "版本更新",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/setting/update/index",
              children: [
                {
                  path: "/setting/update/index",
                  name: "SettingUpdateIndex",
                  meta: {
                    title: "更新记录",
                    hidden: false,
                  },
                },
              ],
            },
            {
              path: "/setting/role",
              name: "SettingRole",
              meta: {
                title: "权限管理",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/setting/role/member",
              children: [
                {
                  path: "/setting/role/member",
                  name: "SettingMemberIndex",
                  meta: {
                    title: "管理员维护",
                    hidden: false,
                  },
                },
                {
                  path: "/setting/role/record",
                  name: "SettingRecordIndex",
                  meta: {
                    title: "操作日志",
                    hidden: false,
                  },
                },
                {
                  path: "/setting/role/index",
                  name: "SettingRoleIndex",
                  meta: {
                    title: "角色管理",
                    hidden: false,
                  },
                },
                {
                  path: "/setting/role/index_edit",
                  name: "SettingRoleIndexEdit",
                  meta: {
                    title: "编辑角色",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
        {
          path: "/feedback",
          name: "Feedback",
          meta: {
            title: "意见反馈",
            hidden: false,
          },
          redirect: "/feedback/detail",
          component: {
            components: {
              "sub-menu": {
                name: "SubMenu",
                props: {
                  menuInfo: {
                    0: false,
                    1: true,
                  },
                },
                __hmrId: "545792ca",
                __file: "src/components/layout/SubMenu.vue",
              },
            },
            __scopeId: "data-v-434c5603",
            __hmrId: "434c5603",
            __file: "src/components/layout/BasicLayout.vue",
          },
          children: [
            {
              path: "/feedback/detail",
              name: "FeedbackDetail",
              meta: {
                title: "意见反馈详情",
                hidden: false,
              },
              component: {
                __hmrId: "d80dc75a",
                __file: "src/components/layout/RouterView.vue",
              },
              redirect: "/feedback/detail/list",
              children: [
                {
                  path: "/feedback/detail/list",
                  name: "FeedbackDetailList",
                  meta: {
                    title: "意见反馈列表",
                    hidden: false,
                  },
                },
              ],
            },
          ],
        },
      ],
      //       treeData: [
      //   {
      //     title: 'parent 1',
      //     key: '0-0',
      //     children: [
      //       {
      //         title: 'parent 1-0',
      //         key: '0-0-0',
      //         children: [
      //           { title: 'leaf', key: '0-0-0-0', },
      //           { title: 'leaf', key: '0-0-0-1' },
      //         ],
      //       },
      //       {
      //         title: 'parent 1-1',
      //         key: '0-0-1',
      //         children: [{ key: '0-0-1-0', title: 'sss' }],
      //       },
      //     ],
      //   },
      // ],
      expandedKeys: [],
      selectedKeys: [],
      checkedKeys: [],
    });
    const loading = ref(false);
    const visible = ref(false);
    const rules = {
      keyword: [{ required: true, message: "请输入查询内容" }],
    };
    // 表单验证
    const rules1 = {
      roleDesc: [{ required: true, message: "请输入", trigger: "blur" }],
      roleName: [{ required: true, message: "请输入", trigger: "blur" }],
    };
    const editRef = ref("");
    // const editForm = reactive({
    //   isDele: 0,
    // });
    const title = ref("新增管理员");
    // 分页器
    const page = reactive({
      current: 1,
      pageSize: 10,
      total: 0,
    });

    const columns = [
      {
        title: "管理员账号",
        dataIndex: "account",
        className: "table",
        align: "center",
      },
      {
        title: "姓名",
        dataIndex: "realName",
        className: "table",
        align: "center",
        sort: true,
      },
      {
        title: "角色",
        dataIndex: "content",
        className: "table",
        align: "center",
      },
      {
        title: "创建时间",
        dataIndex: "createdAt",
        className: "table",
        align: "center",
      },
      {
        title: "启用",
        dataIndex: "isDele",
        className: "table",
        align: "center",
        slots: {
          customRender: "isDele",
        },
      },
      {
        title: "权限设置",
        dataIndex: "roles",
        className: "table",
        align: "center",
      },
      {
        title: "操作",
        dataIndex: "operation",
        className: "table",
        align: "center",
        fixed: "right",
        slots: {
          customRender: "operation",
        },
      },
    ];

    // 分页器
    const pagination = computed(() => ({
      current: page.current,
      pageSize: page.pageSize,
      pageSizeOptions: ["10", "20", "30"],
      showTotal: (total, range) => {
        return "共" + total + "条";
      },
      total: page.total,
      showQuickJumper: true,
      showSizeChanger: true,
    }));

    // 分页器改变的时候
    const handleTableChange = (pagination, filters, sorter) => {
      console.log(pagination, "当前分页");
      page.current = pagination.current;
      page.pageSize = pagination.pageSize;

      // 请求列表接口
      init();
    };
    // 查询
    const search = () => {
      console.log(form, "表单内容");
      formRef.value
        .validate()
        .then(() => {
          page.current = 1;
          init();
        })
        .catch((error) => {});
    };
    // 停用
    const changeStatus = () => {
      const {closeModal} = useModal.confirm({
        title: "确定停用所选用户吗?",
        icon: createVNode(ExclamationCircleOutlined),
        okText: "确定",
        okType: "danger",
        cancelText: "取消",
      });
    };
    const cancle = () => {
      router.go(-1);
    };
    const add = () => {
      title.value = "新增管理员";
      visible.value = true;
    };
    // 查看
    const look = (e) => {
      // data.id = e;
      initDetail(e);
      title.value = "查看管理员";
      visible.value = true;
    };

    // 编辑
    const edit = (e) => {
      // data.id = e;
      initDetail(e);
      title.value = "编辑管理员";
      visible.value = true;
    };
    //  删除
    const del = () => {
      const {closeModal} = useModal.confirm({
        title: "确定删除当前管理员吗?",
        icon: createVNode(ExclamationCircleOutlined),
        okText: "确定",
        okType: "danger",
        cancelText: "取消",
      });
    };

    const handleCheckAll = (e) => {
      console.log(e.target.checked, "---------=====-------");
      let arr = checkAllMenu(data.treeData, e.target.checked);
      myArr = [];
      let arr2 = filterMy(arr);
      data.checkedKeys = [...arr2];
      data.finalTree = filterFinalMenu(data.treeData, arr2);
    };
    const handleCheck = (arr, e) => {
      data.finalTree = filterFinalMenu(data.treeData, arr);
      console.log(data.finalTree, "finalllllllllll-------");
    };
    const save = () => {
      //   console.log(data.selectedKeys, "---------=====-------");
      //   console.log(data.checkedKeys, "---------=====-------");

      editRef.value
        .validate()
        .then(async () => {
          if (!data.id) {
            if (data.finalTree.length == 0) {
              message.error("请选择权限");
              return;
            }
          }
          let res = await saveMenu({
            editRoleDto: { ...data.editForm },
            menuFirstDtos: [
              ...(data.finalTree.length > 0 ? data.finalTree : data.treeData),
            ],
          });
          if (res.success) {
            router.go(-1);
          }
        })
        .catch((error) => {});
    };
    const initDetail = async (id) => {
      let res = await selectOneAdmins(id);
      if (res.success) {
        data.editForm = res.object;
      }
    };

    init();
    const filterAsyncRoutes = (routes) => {
      const res = [];
      routes.forEach((route) => {
        const tmp = { ...route, title: route.meta.title, key: route.path };

        // 如果有孩子这递归，没孩子跳过下一个路由
        if (tmp.children) {
          tmp.children = filterAsyncRoutes(tmp.children);
        }
        res.push(tmp);
      });

      return res;
    };
    let myArr = [];
    const filterMy = (routes) => {
      routes.forEach((route) => {
        const tmp = { ...route, key: route.id };
        // if(tmp.hasOwnProperty('accessPermissions')){
        if (tmp.title == "查看" && tmp.value == 1) {
          myArr.push(tmp.id);
        }
        // }
        // if(tmp.hasOwnProperty('operationPermissions')){
        if (tmp.title == "操作" && tmp.value == 1) {
          myArr.push(tmp.id);
        }
        // }
        // 如果有孩子这递归，没孩子跳过下一个路由
        if (tmp.children) {
          tmp.children = filterMy(tmp.children);
        }
      });
      return myArr;
    };
    const filterMenu = (routes) => {
      const res = [];
      routes.forEach((route) => {
        const tmp = { ...route, key: route.id };

        // 如果有孩子这递归，没孩子跳过下一个路由
        if (tmp.children) {
          tmp.children = filterMenu(tmp.children);
        }
        res.push(tmp);
      });

      return res;
    };
    const filterFinalMenu = (routes, arr) => {
      const res = [];
      routes.forEach((route) => {
        const tmp = { ...route, key: route.id };
        if (tmp.title == "查看") {
          tmp.value = 0;
          if (arr.indexOf(tmp.id) > -1) {
            tmp.value = 1;
          }
        }
        if (tmp.title == "操作") {
          tmp.value = 0;
          if (arr.indexOf(tmp.id) > -1) {
            tmp.value = 1;
          }
        }
        // 如果有孩子这递归，没孩子跳过下一个路由
        if (tmp.children) {
          tmp.children = filterFinalMenu(tmp.children, arr);
        }
        res.push(tmp);
      });

      return res;
    };
    const checkAllMenu = (routes, flag) => {
      const res = [];
      routes.forEach((route) => {
        const tmp = { ...route, key: route.id };
        if (tmp.title == "查看") {
          tmp.value = flag;
        }
        if (tmp.title == "操作") {
          tmp.value = flag;
        }
        // 如果有孩子这递归，没孩子跳过下一个路由
        if (tmp.children) {
          tmp.children = checkAllMenu(tmp.children, flag);
        }
        res.push(tmp);
      });

      return res;
    };

    async function init() {
      data.id = route.query.id;
      if (data.id) {
        let res = await roleDetail({ roleId: data.id });
        if (res.success) {
          data.treeData = filterMenu(res.object.menuOneGetVo);
          let arr = filterMy(res.object.menuOneGetVo);
          data.checkedKeys = [...arr];
          data.editForm = res.object.role;
          return;
        }
      }
      loading.value = true;
      let res = await menuList();

      if (res.success) {
        console.log(res, "00000000000");
        loading.value = false;

        // data.treeData = filterAsyncRoutes( data.treeData)
        data.treeData = filterMenu(res.object);
      }
    }

    return {
      ...toRefs(data),
      formRef,
      form,
      rules,
      columns,
      loading,
      pagination,
      handleTableChange,
      search,
      edit,
      look,
      del,
      changeStatus,
      editRef,
      // editForm,
      save,
      cancle,
      add,
      title,
      visible,
      labelCol: { style: { fontWeight: "bold", width: "100px" } },
      wrapperCol: { offset: 1 },
      rules1,
      filterAsyncRoutes,
      filterMenu,
      filterFinalMenu,
      checkAllMenu,
      handleCheck,
      handleCheckAll,
    };
  },
};
</script>
